package cn.ac.cintcm.prespring.etl;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import cn.ac.cintcm.prespring.util.Config;
import cn.ac.cintcm.prespring.util.FileUtil;
import cn.ac.cintcm.prespring.util.OracleDb;
import cn.ac.cintcm.prespring.util.StringUtil;

public class ProcessPrescription {
    
    public static void main(String[] args) throws IOException {
        ProcessPrescription pe = new ProcessPrescription();
        //pe.splitTerms();
        //pe.splitWords();
        //List<String> categories = pe.generateCharacterCategory("id", "gongx", "pres_zyfj", "gxn.csv", 1);
        //pe.outputTerms(categories, "gxf2.txt");
        //List<String[]> idNames = pe.getDbIDTerms("originalid", "name", "pres_chc");
        //List<String[]> idSize = pe.countYaows(pe.getFileTerms("prescriptions.txt"));
        List<String[]> idSize = pe.getIds(pe.getFileTerms("prescriptions.txt"));
        pe.outputTermsDb(idSize, "pres_zyfj", "comids",2000);
    }



    public List<String[]> countYaows(List<String> fileTerms) {
        List<String[]> strList = null;
        if (fileTerms != null) {
            strList = new ArrayList<String[]>();
            for (String terms : fileTerms) {
                if (terms != null) {
                    String[] s = terms.split(Config.FIELD_SPLITTER);
                    if (s.length == 2) {
                        s[1] = s[1].split(Config.ITEM_SPLITTER).length + "";
                        strList.add(s);
                    }
                }
            }
        }
        return strList;
    }
    
    public List<String[]> getIds(List<String> fileTerms) {
        List<String[]> strList = null;
        if (fileTerms != null) {
            strList = new ArrayList<String[]>();
            for (String terms : fileTerms) {
                if (terms != null) {
                    String[] s = terms.split(Config.FIELD_SPLITTER);
                    if (s.length == 2) {
                        strList.add(s);
                    }
                }
            }
        }
        return strList;
    }

    public void splitWords() throws IOException {
        List<String> strs = FileUtil.read("zuz.txt");
        //List<String> splitStrs = ShortText.split(strs, 1);
        countTermsFrequency(strs, "zuzfre.txt");
    }
    
    public void splitTerms() {
        List<String> stringSet = getDbTerms("zuz","pres_zyfj");
        List<String> result = ShortText.splitNonChineseCharacter(stringSet);
        outputTerms(result, "zuz.txt");
    }
    
    public List<String>  getDbTerms(String fieldName, String tableName) {
        String sql = "select "+fieldName+" from "+tableName+" where "+fieldName+" is not null";
        List<String> stringSet = OracleDb.getColumn(sql);
        return stringSet;
    }
    
    public List<String[]>  getDbIDTerms(String idName, String fieldName, String tableName) {
        String sql = "select "+ idName + "," +fieldName+" from "+tableName+" where "+fieldName+" is not null";
        List<String[]> stringSet = OracleDb.getColumn(sql, 2);
        return stringSet;
    }    
    
    public void outputTerms(List<String> result, String fileName) {
        try {
            FileUtil.write(new StringBuilder(
                    StringUtil.array2String(result, Config.NEW_LINE)), 
                    fileName);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    
    public void outputTermsDb(List<String[]> result, String tableName, String fieldName, int size) {
        OracleDb.addColumn(tableName, fieldName, size);
        OracleDb.update(result, tableName, fieldName);
    }
    private void outputNumberDb(List<String[]> result, String tableName, String fieldName) {
        OracleDb.addNumberColumn(tableName, fieldName);
        OracleDb.updateNumber(result, tableName, fieldName);
    }    
    public void countTermsFrequency(List<String> result, String fileName) {
        HashMap<String, Integer> map = ShortText.countText(result);
        try {
            FileUtil.write(new StringBuilder(
                    StringUtil.map2String(map, ",", Config.NEW_LINE)), 
                    fileName);
        } catch (IOException e) {
            e.printStackTrace();
        }        
    }
    
    public List<String>  getFileTerms(String fileName) {
        List<String> strs = null;
        try {
            strs = FileUtil.read(fileName);
            
        } catch (IOException e) {
            e.printStackTrace();
        }
        return strs;
    }
    
    /**
     * 
     * @param idName id字段名称
     * @param fieldName 术语字段名称
     * @param tableName 术语所在字段的所在表名
     * @param dictName 用于切分术语的词典
     * @param width 切分步进长度
     * @return
     */
    public List<String[]> generateCharacterCategory(
            String idName, String fieldName, 
            String tableName, String dictName, int width) {
        List<String[]> idTerms = getDbIDTerms(idName, fieldName, tableName);
        Dictionary dict = new Dictionary(dictName);
        if (idTerms != null) {
            for (int i = 0; i < idTerms.size(); i++) {
                String text = idTerms.get(i)[1];
                Set<String> sbCategory = new HashSet<String>();
                int j = 0; 
                for (;(j + width) <= text.length(); j++) {
                    String subStr = text.substring(j, j+width);
                    if (dict.isExist(subStr)) {
                        sbCategory.add(subStr);
                    }
                }
                if (j < text.length()){
                    String subStr = text.substring(j, text.length());
                    if (dict.isExist(subStr)) {
                        sbCategory.add(subStr);
                    }
                }
                if (sbCategory.size() > 0) {
                    idTerms.get(i)[1] = 
                        StringUtil.array2String(
                                Arrays.asList(sbCategory.toArray(new String[0])), 
                                                Config.ITEM_SPLITTER);
                    
                } else {
                    idTerms.get(i)[1] = "";
                }
            }
        }
        return idTerms;
    }
}
